package com.mzy.multithreading.collect;

import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/8/8 7:35
 * @Version 1.0
 */
public class HashMapThreadSafe {
    public static void main(String[] args) {
//        HashMap<String, String> map = new HashMap<>();

        // 解决方案 ConcurrentHashMap 解决    **常用**
        ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
        for (int i = 0; i < 30; i++) {
            String key = String.valueOf(i);
            new Thread(() -> {
                // 向集合中添加内容
                map.put(key,UUID.randomUUID().toString().substring(0, 8));
                // 从集合中获取内容
                System.out.println(map);
            }, String.valueOf(i)).start();
        }
    }
}
